package com.ztf.room.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztf.room.bean.ActApplyResults;
import com.ztf.room.bean.ActApplys;
import com.ztf.room.bean.Activities;
import com.ztf.room.bean.Teachers;
import com.ztf.room.dao.ActivityDao;
import com.ztf.room.exception.BusinessException;
import com.ztf.room.service.ActivityService;
import com.ztf.room.utils.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ActivityServiceImp implements ActivityService {

    @Autowired
    private ActivityDao activityDao;

    @Override
    public PageInfo<ActApplys> getActivityList(Integer studentId, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<ActApplys> list = activityDao.getActivityList(studentId);

        return PageInfo.of(list);
    }

    @Transactional
    @Override
    public void addActivityApply(ActApplys actApplys, Activities activities) {
        int i = activityDao.addActivity(activities);
        if (i==0){
            throw new BusinessException("创建活动失败");
        }
        activityDao.addActivityApply(actApplys);
    }

    @Override
    public PageInfo<ActApplys> getApplyList(int page, int limit) {
        PageHelper.startPage(page,limit);
        List<ActApplys> list = activityDao.getApplyList();
        return PageInfo.of(list);
    }

    @Transactional
    @Override
    public void saveApplyActivity(ActApplys actApplys, Teachers teachers) {
        int i = activityDao.agreeApplyActivity(actApplys);
        if (i==0){
            // 错误修改
            throw new BusinessException("错误修改或活动不存在");
        }
        ActApplys applys =  activityDao.selectApplyActivityById(actApplys.getActApplyId());
        ActApplyResults applyResults = new ActApplyResults();
        BeanUtils.copyProperties(applys,applyResults);
        BeanUtils.copyProperties(teachers,applyResults);
        applyResults.setApplyresultsDate(TimeUtils.nowTime())
                .setApproveOrnot("1");
        activityDao.addActivityResult(applyResults);
    }
}
